package cn.tedu.nybikeT.controller;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // 配置该类是SpringMVC的Controller类
@RequestMapping("/realtimeData") // 配置该类中所有方法映射的父路径
public class RealtimeDataController {
	
	@RequestMapping(value= {"/infoData"}, produces="application/json;charset=UTF-8")
	@ResponseBody // 指名当前方法直接返回数据
	public String getInfoData(HttpServletRequest req, HttpServletResponse resp) {
		// 从ServletContext中获取数据
		ServletContext sc=req.getServletContext();
		String infoData=(String) sc.getAttribute("infoData");
		// 返回数据
		if(infoData==null) { // 缓存区中没有数据
			return "{data:{stations:[]}}";
		}
		return infoData;
	}
	
	@RequestMapping(value= {"/statusData"}, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String getStatusData(HttpServletRequest req, HttpServletResponse resp) {
		// 从ServletContext中获取数据
		ServletContext sc=req.getServletContext();
		String statusData=(String) sc.getAttribute("statusData");
		// 返回数据
		if(statusData==null) { // 缓存区中没有数据
			return "{data:{stations:[]}}";
		}
		return statusData;
	}

}
